package com.msl.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.msl.MslApp;
import com.msl.R;
import com.msl.base.BaseActivity;
import com.msl.bean.AbRelationBean;
import com.msl.scan.BroadcastConfig;
import com.msl.tool.ToastUtils;
import java.io.IOException;
import java.lang.reflect.Method;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;

public class RelationActivity extends BaseActivity {

    @Bind(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tvs)
    TextView tvs;
    @Bind(R.id.etcode)
    EditText etcode;
    @Bind(R.id.bt)
    Button bt;
    @Bind(R.id.ll)
    LinearLayout ll;
    @Bind(R.id.tv1)
    TextView tv1;
    @Bind(R.id.tv2)
    TextView tv2;
    @Bind(R.id.tv3)
    TextView tv3;
    @Bind(R.id.tv4)
    TextView tv4;
    @Bind(R.id.tv5)
    TextView tv5;
    @Bind(R.id.tv6)
    TextView tv6;
    private String tag = "RelationActivity";
    private String tempcode="";
    AbRelationBean abRelationBean;

    protected BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
              if (intent.getAction().equals(BroadcastConfig.CUSTOM_NAME)) {
                  tempcode = intent.getStringExtra("scannerdata");
                  waitDialog.show();
                  getcode(MslApp.url+"?action=QueryCode&cid="+MslApp.sSharedPreferences.getString(MslApp.cidk,"")
                          +"&userID="+MslApp.sSharedPreferences.getInt(MslApp.userID,0)
                          +"&storeID="+MslApp.sSharedPreferences.getInt(MslApp.storeID,0)
                          +"&code="+tempcode
                  );
            } else if (intent.getAction().equals("netcsucc")) {
                tvs.setText("网络状态:正常");
            } else if (intent.getAction().equals("netcfail")) {
                tvs.setText("网络状态:异常");
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_relation);
        ButterKnife.bind(this);
        tvTitle.setText("关系查询");
        initBroadcastReciever();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BroadcastConfig.CUSTOM_NAME);
        intentFilter.addAction("netcsucc");
        intentFilter.addAction("netcfail");
        registerReceiver(mBroadcastReceiver, intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBroadcastReceiver);
    }

    void getcode(String url) {
        try {
            Request request = new Request.Builder()
                    .url(url)
                    .build();
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                    waitDialog.dismiss();
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (response.isSuccessful()) {//回调的方法执行在子线程。
                        result = response.body().string();
                        Log.e(tag, "result=" + result);
                        waitDialog.dismiss();
                        handler.sendEmptyMessage(0);
                    } else {
                        Log.e(tag, "获取数据失败了" + response.code());
                        waitDialog.dismiss();
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            waitDialog.dismiss();
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    abRelationBean = gson.fromJson(result, AbRelationBean.class);
                    if(abRelationBean.getData()!=null)
                    {
                        tv2.setText(tempcode);
                        tv4.setText(abRelationBean.getData().getProductName());
                        tv6.setText(abRelationBean.getData().getParentCode());
                    }
                    break;
                default:
                    break;
            }
        }
    };

    @OnClick({R.id.iv_title_left, R.id.bt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.bt:
                String code = etcode.getText().toString();
                if (code.length() == 0) {
                    ToastUtils.showSingleToast(this, "请输入条码");
                    return;
                }
                waitDialog.show();
                tempcode = code;
                getcode(MslApp.url+"?action=QueryCode&cid="+MslApp.sSharedPreferences.getString(MslApp.cidk,"")
                        +"&userID="+MslApp.sSharedPreferences.getInt(MslApp.userID,0)
                        +"&storeID="+MslApp.sSharedPreferences.getInt(MslApp.storeID,0)
                        +"&code="+code
                );
                etcode.setText("");
                break;
        }
    }
}
